<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <div v-if="meta.indexOf('EscortAdd') > -1" class="doumee_table_header">
      <el-button
        v-preventReClick
        type="primary"
        @click="addBtn"
      >{{ $t('message.add') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
    >
      <el-table-column align="center" :label="$t('message.bykmc')" prop="name" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.bdlx')" prop="type" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="count-style">{{ scope.row.type == '0' ? $t('message.tc') : scope.row.type == '1'? $t('message.kc'):'' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.bdcp')" prop="objName" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.price')" prop="price" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.zffsl')" prop="circulationAll" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.rfsl')" prop="circulationDay" min-width="100" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.zyssl')" prop="sellNum" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.cjr')" prop="createName" min-width="120" show-overflow-tooltip />
      <el-table-column
        align="center"
        :label="$t('message.createTime')"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.status')"
        min-width="70"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="'1'"
            :inactive-value="'0'"
            :disabled="meta.indexOf('EscortEditSt') === -1"
            @change="upDateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('message.caozuo')"
        min-width="150"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('EscortEdit') > -1"
              v-preventReClick
              type="text"
              @click="toEdit(scope.row.id)"
            >{{ $t('message.edit') }}</el-button>
            <el-button
              v-if="meta.indexOf('EscortDel') > -1"
              v-preventReClick
              type="text"
              class="secondColor"
              @click="deleteRow(scope.row.id)"
            >{{ $t('message.del') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <Pagination
        :total="totalCount"
        :page="pagination.page"
        @current-change="getList"
        @SizeChange="handleSizeChange"
      />
    </div>
    <EditDetail v-if="isEdit" ref="EditRef" @close="closeBtn" @success="closeSuccess" />
  </div>
</template>

<script>
import {
  escortObjList,
  escortPageList,
  escortEditStatus,
  escortDel,
  escortEditDetail
} from '@/api'
import { parseTime } from '@/utils'
import { queryFormConfig } from './config'
import Pagination from '@/components/paginations'
import QueryForm from '@/components/QueryForm'
import EditDetail from './edit.vue'
export default {
  name: 'EscortPageList',
  components: {
    QueryForm,
    EditDetail,
    Pagination
  },
  data() {
    return {
      meta: [],
      list: [],
      totalCount: 10,
      listLoading: false,
      pagination: {
        page: 1,
        rows: 10
      },
      isEdit: false,
      queryFormConfig,
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getPzList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    getPzList() {
      escortObjList({
        param: {
          venueId: sessionStorage.getItem('venueId'), queryType: '1'
        }
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.queryFormConfig.formItems[1].options = res.recordList || []
        })
        .catch(() => {})
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    closeBtn() {
      this.isEdit = false
      this.getList()
    },
    closeSuccess() {
      this.isEdit = false
      this.$set(this.filters, 'objId', '')
      this.getPzList()
      this.getList()
    },
    getList(page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = Number(page)
      }
      pagination.firstQueryTime = parseTime(new Date())
      this.listLoading = true
      escortPageList({
        pagination,
        param: {
          ...filters,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
            if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
              this.getList(Math.ceil(res.totalCount / pagination.rows))
            }
            this.$refs.table.doLayout()
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    clear() {
      this.filters = { time: [] }
      this.getList(1)
    },
    toEdit(id) {
      escortEditDetail({
        param: {
          id
        }
      })
        .then((res) => {
          if (res.errorCode === '000000') {
            const obj = res.record
            if (obj.type === '1') {
              obj.objId2 = obj.objId
            }
            this.isEdit = true
            this.$nextTick(() => {
              this.$refs.EditRef.showClick(obj)
            })
          }
        })
        .catch(() => {})
    },
    upDateStatus(row) {
      // 更新状态
      const arr = this.list
      const obj = { ...row }
      this.listLoading = true
      escortEditStatus({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            if (obj.status === '1') {
              this.$message.success(this.$t('message.qycg'))
            } else {
              this.$message.success(this.$t('message.jycg'))
            }
            this.getList()
          } else {
            arr.forEach((e) => {
              if (e.id === obj.id) {
                e.status = row.status === '1' ? '0' : '1'
              }
            })
            this.list = arr
          }
        })
        .catch((err) => {
          this.listLoading = false
          arr.forEach((e) => {
            if (e.id === obj.id) {
              e.status = row.status === '1' ? '0' : '1'
            }
          })
          this.list = arr
          console.log(err)
        })
    },
    deleteRow(id) {
      // 删除器材
      this.$confirm(this.$t('message.scbyktc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          escortDel({
            param: {
              id: id
            }
          })
            .then((res) => {
              if (res.errorCode === '000000') {
                this.$message.success(this.$t('message.delcg'))
                this.getList()
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    addBtn() {
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.EditRef.showClick()
      })
    }
  }
}
</script>

